# -*- codeing:utf-8 -*-
'''
@author: jiaoyulong
@datetime: 2024/3/15 15:21
@Blog:
'''
# from dataclasses import dataclass
#
#
# @dataclass
# class Test:
#     name: str
#     age: int = None
#
#     def __post_init__(self):
#         """用于在对象初始化之后执行一些额外操作"""
#         if self.age is None:
#             self.age = 18
#
#         print(f'姓名：{self.name}, 年龄：{self.age}')
#
#
# if __name__ == '__main__':
#     t = Test(name='JAO')

import sys
import time

from PyQt5.QtCore import QProcess, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QTextEdit
from multiprocessing import Pool
from multiprocessing import Process, Queue
import time
import dolphindb as ddb


class MyProcess(Process):
    def __init__(self, loop, share_queue):
        Process.__init__(self)
        self.loop = loop
        self.share_queue = share_queue

    def run(self):
        self.db = ddb.session()
        self.db.connect("100.125.11.222", 8848, 'jiaoyulong', 'jyl789789')

        df = self.db.run("""
        dt = loadTable("dfs://RQData", "factor_exposure_sws_cne6")
        select * from dt where date==date(2024.03.01)
        """)
        print(df)

        self.db.close()

        for count in range(self.loop):
            time.sleep(1)
            print(f'Pid:{self.pid} LoopCount: {count}')


# 主窗口类
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

        self.q = Queue()

        self.db = ddb.session()
        self.db.connect("100.125.11.222", 8848, 'jiaoyulong', 'jyl789789')

    def init_ui(self):
        self.setWindowTitle('IPC Example')
        self.button = QPushButton('Start', self)
        self.button.clicked.connect(self.start_worker)
        self.text_edit = QTextEdit(self)
        layout = QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.text_edit)
        widget = QWidget(self)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def start_worker(self):
        self.button.setEnabled(False)  # 首先禁用按钮

        # p = Process(target=fun, args=(10, 20, self.q))  # Is QProcess capable of doing this?
        # p.start()
        # # p.join()
        # print(f'完成')

        for i in range(2, 5):
            p = MyProcess(2, self.q)
            p.start()


        # while True:
        #     if not self.q.empty():
        #         print(self.q.get())
        #         time.sleep(1)
        #     else:
        #         print('空')
        #         print(p.is_alive())

        self.button.setEnabled(True)

def f(x, q):
    ret = x ** x
    q.put(ret)
    print(f'x * x = {ret}')

def fun(a, b, q):
    ret = a + b
    q.put(ret)
    print(f'a + b = {ret}')


# if __name__ == '__main__':
#     # multiprocessing.freeze_support()
#     app = QApplication(sys.argv)
#     window = MainWindow()
#     window.show()
#     sys.exit(app.exec_())

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QShortcut
from PyQt5.QtGui import QKeySequence
from PyQt5.QtCore import Qt


# class MainWindow(QMainWindow):
#     def __init__(self):
#         super().__init__()
#         self.button = QPushButton('Click Me')
#         self.button.clicked.connect(self.on_button_clicked)
#         self.setCentralWidget(self.button)
#         shortcut = QShortcut(QKeySequence('Ctrl+B'), self)
#         shortcut.activated.connect(self.button.click)
#
#     def on_button_clicked(self):
#         print('Button clicked')
#
#
# app = QApplication([])
# window = MainWindow()
# window.show()
# app.exec_()

